#include "Log.hpp"
#include <cstdarg>
#include <fstream>
using namespace std;

const char*    Log:: file_name;
std::ofstream* Log:: ofs;
bool           Log:: enabled = false;


bool Log:: open (const char* name)
{
	ofs = new ofstream (name);
	if (!ofs) {
		return false;
	}
    enabled = true;

	print ("Start logging.\n");
	return true;
}

void Log:: close ()
{
    if (!enabled) {
        return;
    }

	if (ofs) {
		print ("End logging.\n");
		ofs->close ();
	}
    enabled = false;

	delete ofs;
}


void Log::print (const char* format, ...)
{
    if (!enabled) {
        return;
    }

	char buf[256];
	va_list args;
	va_start (args, format);
	vsnprintf (buf, 256, format, args);
	va_end (args);

	int len = strnlen (buf, 256);
	ofs->write (buf, len);
}



